package com.zqcy.workbench.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.shinemo.base.core.utils.Handlers;
import com.shinemo.minisdk.util.ColorUtils;
import com.zqcy.workbench.databinding.ActivityCodeLoginBinding;
import com.zqcy.workbench.utils.Utils;
import com.zqcy.workbench.utils.Constants;
import com.zqcy.workbench.utils.MyTextWatcher;

public class CodeLoginActivity extends AppCompatActivity {

    private ActivityCodeLoginBinding mCodeLoginBinding;
    private TextView[] mTvInputs;

    private int againTime = 59;

    public static void startActivity(Context context, String phone) {
        Intent intent = new Intent(context, CodeLoginActivity.class);
        intent.putExtra("phone", phone);
        context.startActivity(intent);
    }

    @SuppressLint("SetTextI18n")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mCodeLoginBinding = ActivityCodeLoginBinding.inflate(LayoutInflater.from(this));
        setContentView(mCodeLoginBinding.getRoot());
        //设置全屏模式
        ColorUtils.setTransparent(this, Constants.isDarkModel, 1);
        mCodeLoginBinding.tvCodeDesc.setText("验证码已发送至 " + Utils.getFormatPhone(getIntent().getStringExtra("phone")));

        mTvInputs = new TextView[]{mCodeLoginBinding.tvCode1, mCodeLoginBinding.tvCode2, mCodeLoginBinding.tvCode3,
                mCodeLoginBinding.tvCode4, mCodeLoginBinding.tvCode5, mCodeLoginBinding.tvCode6};

        mCodeLoginBinding.edtCodeInput.addTextChangedListener(new MyTextWatcher() {
            @SuppressLint("SetTextI18n")
            @Override
            public void afterTextChanged(Editable editable) {
                clearInput();
                String s = editable.toString();
                for (int i = 0; i < s.length(); i++) {
                    mTvInputs[i].setText(s.charAt(i) + "");
                }
            }
        });

        mCodeLoginBinding.llCodeContainer.setOnClickListener(view -> {
            mCodeLoginBinding.edtCodeInput.setFocusable(true);
            mCodeLoginBinding.edtCodeInput.setFocusableInTouchMode(true);
            mCodeLoginBinding.edtCodeInput.requestFocus();
            Utils.showSoftKeyBoard(CodeLoginActivity.this, mCodeLoginBinding.edtCodeInput);
        });

//        if (BuildConfig.DEBUG) {
//            againTime = 10;
//        }
        Handlers.postDelayed(timer, 1000);

        mCodeLoginBinding.tvSendAgain.setOnClickListener(view -> {
            mCodeLoginBinding.tvSendAgain.setVisibility(View.GONE);
            mCodeLoginBinding.tvAgainTime.setText("60s");
            mCodeLoginBinding.tvAgainTime.setVisibility(View.VISIBLE);
            mCodeLoginBinding.tvTimeDesc.setVisibility(View.VISIBLE);
            againTime = 59;
            Handlers.postDelayed(timer, 1000);
        });
    }

    private void clearInput() {
        for (TextView textView : mTvInputs) {
            textView.setText("");
        }
    }

    @Override
    protected void onStop() {
        Utils.hideKeyBoard(CodeLoginActivity.this);
        super.onStop();
    }

    private final Runnable timer = this::showAgainTime;

    @SuppressLint("SetTextI18n")
    private void showAgainTime() {
        if (isDestroyed()) {
            return;
        }
        mCodeLoginBinding.tvAgainTime.setText(againTime + "s");
        againTime--;
        if (againTime == 0) {
            mCodeLoginBinding.tvAgainTime.setVisibility(View.GONE);
            mCodeLoginBinding.tvTimeDesc.setVisibility(View.GONE);
            mCodeLoginBinding.tvSendAgain.setVisibility(View.VISIBLE);
        } else {
            Handlers.postDelayed(timer, 1000);
        }
    }
}